package 链表题.合并有序链表;

import 链表题.ListNode;

/**
 * 方法二: 递归调用
 */

public class Test2 {

    public static ListNode mergeTwoLists(ListNode p1, ListNode p2) {
        if (p1 == null) {
            return p2;
        }
        if (p2 == null) {
            return p1;
        }
        if (p1.val < p2.val) {
            p1.next = mergeTwoLists(p1.next, p2);
            return p1;
        } else {
            p2.next = mergeTwoLists(p1, p2.next);
            return p2;
        }
    }

    public static void main(String[] args) {
        ListNode n1 = ListNode.of(1, 2, 3, 5, 6, 9, 10);
        ListNode n2 = ListNode.of(0, 4, 7, 8);
        System.out.println(n1);
        System.out.println(n2);
        ListNode n = mergeTwoLists(n1, n2);
        System.out.println(n);
    }

}
